package br.com.sga.util.conexao;

import br.com.sga.erro.ConexaoException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

/**
 *
 * @author kenzo
 */
public class DataSourcePostgres implements DataSource{
    private String driver,url,usuario,senha;
    
    public DataSourcePostgres(String driver, String url, String usuario, String senha){
        super();
        this.driver = driver;
        this.url = url;
        this.usuario = usuario;
        this.senha = senha;
    }
    
    public DataSourcePostgres(ResourceBundle rb){
        super();
        this.driver = rb.getString("driver");
        this.url = rb.getString("url");
        this.usuario = rb.getString("usuario");
        this.senha = rb.getString("senha");
    }
    
    @Override
    public Connection getConnection() throws ConexaoException {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url, usuario, senha);
        } catch (ClassNotFoundException | SQLException e) {
            throw new ConexaoException(e);
        }
    }

    @Override
    public void closeConnection(Connection c) throws ConexaoException {
        try {
            c.close();
        } catch (SQLException e) {
            throw new ConexaoException(e);
        }
    }
    
}
